<template>
  <section class="my-box" :style="{ backgroundImage: `url(${myBg})` }">
    <div class="user-top">
      <div class="user-msg">
        <!-- <div class="avatar">
          <img width="65" :src="identity == 'company' ? avatar_company : avatar_user" alt="用户头像" />
        </div> -->
        <div class="user-name" v-if="accessToken">
          <div
            v-if="identity == 'company' && companyUserDetail"
          >{{ companyUserDetail.zbCompanyResponse.name }}</div>
          <div v-if="identity == 'user' && userDetail">{{ userDetail.name }}</div>
          <div class="user_validation">
            <div
              v-if="
                identity == 'company' &&
                companyUserDetail.zbCompanyResponse.auditStatus == 'AUDITED'
              "
            >
              <span>企业资质</span>
              <img width="12" :src="user_validation" alt="用户头像" />
            </div>
            <div v-if="identity == 'user' && userDetail.dataPerfection">
              <span>个人资质</span>
              <img width="12" :src="user_validation" alt="用户头像" />
            </div>
          </div>
        </div>
        <div class="to_login" v-else @click="$router.push('login')">登录/注册</div>
      </div>
      <div class="modify-user-information" v-if="accessToken">
        <!-- <div class="change_user_roles">
          切换身份
          <img width="73px" :src="modify_user_information" alt="修改用户信息" />
        </div>-->
        <div class="perfect_information" @click="handlleRedirect">
          完善资料
          <van-icon class="filter-items-icons" name="arrow" />
        </div>
        <div class="perfect_information" @click="$router.push('/realName')">
          实名认证
          <van-icon class="filter-items-icons" name="arrow" />
        </div>
      </div>
    </div>

    <div class="task_information" @click="$router.push('issuedTask')" v-if="identity != 'user'">
      <span>
        已发布任务
        <i v-if="accessToken">：{{ jobCount }}</i>
      </span>
      <span class="new_task">
        <!-- <span v-if="accessToken">有新的任务进度</span> -->
        <van-icon class="filter-items-icons" name="arrow" />
      </span>
    </div>

    <div class="task_information" @click="$router.push('haveTask')" v-if="identity != 'company'">
      <span>
        已接任务
        <i v-if="accessToken">：{{ userJobCount }}</i>
      </span>
      <span class="new_task">
        <!-- <span v-if="accessToken">有新的任务进度</span> -->
        <van-icon class="filter-items-icons" name="arrow" />
      </span>
    </div>

    <div class="log_out ling-linearPinkButton" @click="handleLogout" v-if="accessToken">退出登录</div>
  </section>
</template>

<script>
import myBg from '@/assets/images/my-bg.png'
import avatar_company from '@/assets/images/avatar_company.png'
import avatar_user from '@/assets/images/avatar_user.png'
import user_validation from '@/assets/images/user_validation.png'
import modify_user_information from '@/assets/images/modify_user_information.png'
import {
  getIdentity,
  setIdentity,
  setUserInfo,
  getAccessToken,
} from '@/utils/userAuth'
import { createNamespacedHelpers } from 'vuex'
const { mapState, mapActions } = createNamespacedHelpers('myData')

export default {
  name: 'MyPage',
  data() {
    return {
      myBg,
      avatar_company,
      avatar_user,
      user_validation,
      modify_user_information,
      accessToken: getAccessToken(),
      identity: getIdentity(),
    }
  },
  components: {},
  inject: ['reload'],
  computed: {
    ...mapState([
      'jobCount',
      'userJobCount',
      'companyUserDetail',
      'userDetail',
    ]),
  },
  // watch: {
  //   accessToken: {
  //     handler(newVal, oldVal) {
  //       return newVal
  //     },
  //     deep: true
  //   }
  // },
  created() {
    if (this.identity == 'company') this.getJobCount()
    if (this.identity == 'user') this.getUserJobCount()
  },
  methods: {
    ...mapActions(['getJobCount', 'getUserJobCount']),
    handlleRedirect() {
      if (getIdentity() == 'company') {
        this.$router.push('improvementCompany')
      } else {
        this.$router.push('improvementPersonal')
      }
    },
    handleLogout() {
      const params = {
        accessToken: '',
        refreshToken: '',
        expireTime: '',
        id: '',
      }
      setUserInfo(params)
      setIdentity()
      this.reload()
    },
  },
}
</script>

<style lang='less' scoped>
.my-box {
  height: 100vh;
  background-size: contain;
  background-repeat: no-repeat;
  padding-top: 32px;
  .user-top {
    display: flex;
    justify-content: space-between;
    margin-bottom: 65px;
    .user-msg {
      display: flex;
      justify-content: space-between;
      height: 65px;
      margin-top: 52px;
      margin-left: 40px;
      .user-name {
        margin-left: 14px;
        margin-top: 10px;
        font-size: 14px;
        color: white;
        line-height: 25px;
        .user_validation {
          font-size: 12px;
          span {
            margin-right: 10px;
          }
          img {
            border-radius: 50%;
            display: inline-block;
          }
        }
      }
      .to_login {
        font-size: 14px;
        margin-left: 14px;
        color: white;
        line-height: 65px;
      }
    }
    .modify-user-information {
      width: 100px; //拿出切换身份之后要删掉
      color: white;
      margin-top: 5px;
      display: flex;
      flex-direction: column;
      justify-content: space-between;
      .change_user_roles {
        font-size: 10px;
        img {
          position: relative;
          top: 3px;
        }
      }
      .perfect_information {
        font-size: 14px;
        margin-left: 5px;
        .filter-items-icons {
          font-size: 10px;
          position: relative;
        }
      }
    }
  }

  .task_information {
    width: 335px;
    margin: 0 auto;
    margin-bottom: 10px;
    height: 60px;
    line-height: 60px;
    background: rgba(255, 255, 255, 1);
    box-shadow: 0px 6px 20px rgba(122, 197, 239, 0.2);
    opacity: 1;
    border-radius: 8px;
    display: flex;
    justify-content: space-between;
    font-size: 14px;
    color: #333333;
    padding: 0 20px;
    span {
      i {
        font-style: normal;
      }
    }
    .new_task {
      color: #7ac5ef;
      font-size: 12px;
      .filter-items-icons {
        font-size: 10px;
        color: #666666;
        position: relative;
        top: 1px;
      }
    }
  }
  .log_out {
    width: 155px;
    height: 45px;
    line-height: 45px;
    position: absolute;
    bottom: 136px;
    left: 50%;
    margin-left: -77px;
  }
}
</style>